﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;


namespace VirtualEvaluator
{
    public partial class CredentialADForm : Form
    {
        private List<string> domains = new List<string>();

        public CredentialADForm()
        {
            RetrieveAvailableAD();
            InitializeComponent();
            foreach (string domain in domains)
            {
                comboBoxActiveDir.Items.Add(domain);
            }
        }
        /// <summary>
        /// Retrieves all available AD Domains from Forest.
        /// </summary>
        private void RetrieveAvailableAD()
        {
            System.DirectoryServices.ActiveDirectory.DomainCollection domainCollection;
            try
            {
                using (System.DirectoryServices.ActiveDirectory.Forest forest 
                    = System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest())
                {
                    domainCollection = forest.Domains;
                    foreach (System.DirectoryServices.ActiveDirectory.Domain domain in domainCollection)
                    {
                        domains.Add(domain.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
            }
            
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            try
            {
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            try
            {
                String password = "";
                String userName = "";

                this.textBoxUserName.Text = userName;
                this.textBoxPassword.Text = password;
                
                new MainForm().Show();
                this.Hide();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
            }
        }
    }
}
